package com.cwg.dict.ui.cli.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.ynu.sei.dict.service.core.IDictQueryService;
import cn.edu.ynu.sei.dict.service.core.Word;

import com.cwg.dict.stem.model.StemType;
import com.cwg.dict.stem.model.Token;
import com.cwg.dict.stem.service.Translator;
import com.cwg.dict.stem.service.WordParser;
import com.cwg.dict.ui.cli.DictionaryLayout;

abstract class AbstractDictLayout implements DictionaryLayout {

	protected static final String	SEPARATOR	= "";

	protected String				inputWord;

	@Override
	public String[] getSeparator() {
		return new String[] { SEPARATOR, SEPARATOR };
	}

	public void input(String word) {
		this.inputWord = word;
	}

}

class DictLayout extends AbstractDictLayout {

	private IDictQueryService	dicService;

	@Override
	public String[] getContent() {
		assert inputWord != null : "input word is required!";
		Word results = getStardictReader().lookup(inputWord);
		return new String[] { results.self, results.definition };
	}

	public IDictQueryService getDicService() {
		return dicService;
	}

	@Override
	public String[] getHeader() {
		try {
			return new String[] { "=== " + dicService.lookup(inputWord).defComeFrom + " ===" };
		} catch (Exception e) {
			return new String[0];
		}
	}

	public IDictQueryService getStardictReader() {
		return dicService;
	}

	public void setDicService(IDictQueryService dicService) {
		this.dicService = dicService;
	}
}

class StemDictLayout extends AbstractDictLayout {

	private Logger		logger	= LoggerFactory.getLogger(StemDictLayout.class);
	private Translator	translator;
	private WordParser	wordParser;

	@Override
	public String[] getContent() {
		List<String> results = new ArrayList<String>();
		results.add(inputWord);
		results.add(translator.toChinese(inputWord));
		results.add(SEPARATOR);
		Token[] tokenRows = wordParser.query(inputWord);
		results.addAll(tokenInfoSection(tokenRows));
		return results.toArray(new String[results.size()]);
	}

	@Override
	public String[] getHeader() {
		return new String[] { "=== Stem Dictionary ===" };
	}

	public Translator getTranslator() {
		return translator;
	}

	public WordParser getWordParser() {
		return wordParser;
	}

	public void setTranslator(Translator translator) {
		this.translator = translator;
	}

	public void setWordParser(WordParser wordParser) {
		this.wordParser = wordParser;
	}

	private List<String> tokenInfoSection(Token[] tokens) {
		List<String> results = new ArrayList<String>();
		String pattern;
		for (Token token : tokens) {
			StemType category = token.getGroup().getSection().getCategory();
			switch (category) {
			case PREFIX:
				pattern = "%s- = %s %s %s";
				break;
			case STEM:
				if (token.isBasicWord()) {
					pattern = "%s %s %s %s";
				} else {
					pattern = "%s = %s %s %s";
				}
				break;
			case SUFFIX:
				pattern = "-%s %s %s %s";
				break;
			default:
				pattern = "%s = %s %s %s";
				break;
			}

			logger.debug("using pattern {}", pattern);

			String original;
			String translate;
			String description;
			if (StemType.SUFFIX == category) {
				original = StringUtils.defaultIfEmpty(token.getGroup().getSection().getDescription(), "");
			} else {
				original = StringUtils.defaultIfEmpty(token.getOriginal(), "");
			}
			translate = StringUtils.defaultIfEmpty(token.getTranslate(), "");
			if (token.getDescription() == null) {
				description = "";
			} else {
				description = "(" + token.getDescription() + ")";
			}

			results.add(String.format(pattern, token.getName(), original, translate, description));
		}
		return results;
	}

}
